﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Printing;
using System.Text;
using System.Windows.Forms;
using RamaSoft.Common;
using ComponentFactory.Krypton.Toolkit;

namespace RamaSoft.CtlPatrim
{
    public partial class Contábil : KryptonForm
    {
        Database myDBase;
        cadsGerais rolDepto;
        int queAno, qRow, qtItens, nmPag;

        public Contábil(Database myDBase)
        {
            InitializeComponent();
            this.myDBase = myDBase;
        }

        private void Form_Load(object sender, EventArgs e)
        {
            Depreçia regDepr = new Depreçia(myDBase);
            int dbRet = regDepr.Mínima();
            if (dbRet != 0)
            {
                MessageBox.Show("Favor avisar a RamaSoft\n"
                    + "referência: " + this.Name + " Mínima\n" + regDepr.MsgErro,
                    "Erro no acesso ao Banco de Dados", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            queAno = regDepr.Data.Year;
            rolDepto = new cadsGerais(myDBase);
            rolDepto.Tipo = "D";
            dbRet = rolDepto.ListaNome();
            if (dbRet != 0)
            {
                MessageBox.Show("Favor avisar a RamaSoft\n"
                    + "referência: " + this.Name + " Depto\n" + rolDepto.MsgErro,
                    "Erro no acesso ao Banco de Dados", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            Starta.Enabled = true;
        }

        private void Monta_Lista()
        {
            for (int dp = 0; dp < rolDepto.Items.Count; dp++)
            {
                string queDepto = rolDepto.Items[dp].Descrição.Trim();
                bool Listou = false;
                double totDepreç = 0;
                Bens rolBens = new Bens(myDBase);
                rolBens.Departamento = rolDepto.Items[dp].Código;
                int dbRet = rolBens.ListaDepto();
                if (dbRet != 0)
                {
                    MessageBox.Show("Favor avisar a RamaSoft\n"
                        + "referência: " + this.Name + " Bens\n" + rolBens.MsgErro,
                        "Erro no acesso ao Banco de Dados", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
                for (int xxAno = queAno; xxAno <= DateTime.Today.Year; xxAno++)
                {
                    double totValor = 0;
                    for (int nn = 0; nn < rolBens.Items.Count; nn++)
                    {
                        if (rolBens.Items[nn].Data.Year <= xxAno)
                        {
                            Depreçia regDepr = new Depreçia(myDBase);
                            regDepr.Código = rolBens.Items[nn].Código;
                            dbRet = regDepr.GetDepreç(xxAno);
                            if (dbRet != 0)
                            {
                                MessageBox.Show("Favor avisar a RamaSoft\n"
                                    + "referência: " + this.Name + " Depreç\n" + regDepr.MsgErro,
                                    "Erro no acesso ao Banco de Dados", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                return;
                            }
                            totValor = totValor + rolBens.Items[nn].Valor;
                            totDepreç = totDepreç + regDepr.Depreço;
                        }
                    }
                    double saldo = totValor - totDepreç;
                    if (totValor > 0 || totDepreç > 0)
                    {
                        listaDepto.Rows.Add(queDepto, xxAno.ToString(), totValor.ToString("##,##0.00"),
                            totDepreç.ToString("##,##0.00"), saldo.ToString("##,##0.00"));
                        if (queDepto.Length > 0)
                        {
                            int qRow = listaDepto.Rows.Count - 1;
                            listaDepto.Rows[qRow].DefaultCellStyle.BackColor = SystemColors.Info;
                            listaDepto.CurrentCell = listaDepto.Rows[qRow].Cells[0];
                        }
                        this.Refresh();
                        Listou = true;
                        queDepto = "";
                    }
                    totValor = 0;
                }
                if (Listou) listaDepto.Rows.Add("- - - - - - - - - - - -");
                totDepreç = 0;
            }
            titExec.Visible = false;
            listaDepto.CurrentCell = listaDepto.Rows[0].Cells[0];
        }

        private void cmdPrint_Click(object sender, EventArgs e)
        {
            qtItens = listaDepto.Rows.Count;
            qRow = 0;
            PrintDocument docto = new PrintDocument();
            docto.PrintPage += new PrintPageEventHandler(this.Docto_PrintPage);
            docto.Print();
        }

        private void Docto_PrintPage(object sender, PrintPageEventArgs ev)
        {
            Pen myPen = new Pen(Brushes.Black);
            Point pt1 = new Point(30, 53);
            Point pt2 = new Point(780, 53);
            ev.Graphics.DrawLine(myPen, pt1, pt2);
            Font myFont1 = new Font("Arial", 14);
            string loja = "Parque das Águas Quentes";
            ev.Graphics.DrawString(loja, myFont1, Brushes.Black, 30, 30);
            ev.Graphics.DrawString("Depreciação do Patrimônio", myFont1, Brushes.Black, 360, 30);
            ev.Graphics.DrawString(DateTime.Today.ToString("dd/MM/yyyy"), myFont1, Brushes.Black, 680, 30);
            if (qRow > 0)
            {
                nmPag = nmPag + 1;
                ev.Graphics.DrawString("pág. " + nmPag.ToString(), myFont1, Brushes.Black, 720, 55);
            }
            Font myFont3 = new Font("Lucida Sans", 10, FontStyle.Underline);
            ev.Graphics.DrawString("Departamento", myFont3, Brushes.Black, 30, 90);
            ev.Graphics.DrawString("Ano", myFont3, Brushes.Black, 288, 90);
            ev.Graphics.DrawString("Total", myFont3, Brushes.Black, 410, 90);
            ev.Graphics.DrawString("Depreciação", myFont3, Brushes.Black, 510, 90);
            ev.Graphics.DrawString("Saldo", myFont3, Brushes.Black, 695, 90);
            Font myFont4 = new Font("Lucida Sans", 10);
            Font myFont2 = new Font("Lucida Console", 10);
            int yPos = 0;
            for (int nmLin = 0; nmLin < 58; nmLin++) 
            {
                if (qRow >= qtItens) break;
                yPos = (nmLin * 16) + 110;
                if (listaDepto.Rows[qRow].Cells[1].Value != null)
                {
                    string edçTotal = listaDepto.Rows[qRow].Cells[2].Value.ToString();
                    edçTotal = edçTotal.PadLeft(12);
                    string edçDepreç = listaDepto.Rows[qRow].Cells[3].Value.ToString();
                    edçDepreç = edçDepreç.PadLeft(12);
                    string edçSaldo = listaDepto.Rows[qRow].Cells[4].Value.ToString();
                    edçSaldo = edçSaldo.PadLeft(12);
                    ev.Graphics.DrawString(listaDepto.Rows[qRow].Cells[0].Value.ToString(),
                        myFont4, Brushes.Black, 30, yPos);
                    ev.Graphics.DrawString(listaDepto.Rows[qRow].Cells[1].Value.ToString(),
                        myFont4, Brushes.Black, 280, yPos);
                    ev.Graphics.DrawString(edçTotal, myFont2, Brushes.Black, 350, yPos);
                    ev.Graphics.DrawString(edçDepreç, myFont2, Brushes.Black, 490, yPos);
                    ev.Graphics.DrawString(edçSaldo, myFont2, Brushes.Black, 630, yPos);
                }
                else ev.Graphics.DrawString(listaDepto.Rows[qRow].Cells[0].Value.ToString(),
                    myFont4, Brushes.Black, 30, yPos);
                qRow = qRow + 1;
            }
            if (qRow >= qtItens) ev.HasMorePages = false;
            else
            {
                ev.Graphics.DrawString("- - - - - - - - - - - - - - - - - - - - - -", myFont1, Brushes.Black, 400, yPos + 14);
                ev.HasMorePages = true;
            }
        }

        private void cmdVoltar_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void Starta_Tick(object sender, EventArgs e)
        {
            Starta.Enabled = false;
            Monta_Lista();
        }
    }
}
